package com.bootdo.fx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.IdWorker;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.DistributionRuleSetupDO;
import com.bootdo.fx.service.DistributionRuleSetupService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class DistributionRuleSetupController {
	@Autowired
	private DistributionRuleSetupService distributionRuleSetupService;
	@Autowired
	private IdWorker idWorker;
	
	@GetMapping("/fx/distributionRuleSetup")
	@RequiresPermissions("fx:distributionRuleSetup:distributionRuleSetup")
	String DistributionRuleSetup(){
	    return "fx/distributionRuleSetup/distributionRuleSetup";
	}
	
	@ResponseBody
	@GetMapping("/fx/distributionRuleSetup/list")
	@RequiresPermissions("fx:distributionRuleSetup:distributionRuleSetup")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DistributionRuleSetupDO> distributionRuleSetupList = distributionRuleSetupService.list(query);
		int total = distributionRuleSetupService.count(query);
		PageUtils pageUtils = new PageUtils(distributionRuleSetupList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/distributionRuleSetup/add")
	@RequiresPermissions("fx:distributionRuleSetup:add")
	String add(){
	    return "fx/distributionRuleSetup/add";
	}

	@GetMapping("/fx/distributionRuleSetup/edit/{ruleId}")
	@RequiresPermissions("fx:distributionRuleSetup:edit")
	String edit(@PathVariable("ruleId") Long ruleId,Model model){
		DistributionRuleSetupDO distributionRuleSetup = distributionRuleSetupService.get(ruleId);
		model.addAttribute("distributionRuleSetup", distributionRuleSetup);
	    return "fx/distributionRuleSetup/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/distributionRuleSetup/save")
	@RequiresPermissions("fx:distributionRuleSetup:add")
	public R save( DistributionRuleSetupDO distributionRuleSetup){
		List<DistributionRuleSetupDO> distributionRuleSetupDOList = distributionRuleSetupService.list(new HashMap<String,Object>(16));
		if(distributionRuleSetupDOList != null && distributionRuleSetupDOList.size() > 0){
			return R.error(300,"分销规则已存在!请勿重复创建.");
		}else {
			distributionRuleSetup.setRuleId(idWorker.nextId() + "");
			if (distributionRuleSetupService.save(distributionRuleSetup) > 0) {
				return R.ok();
			}
			return R.error();
		}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/distributionRuleSetup/update")
	@RequiresPermissions("fx:distributionRuleSetup:edit")
	public R update( DistributionRuleSetupDO distributionRuleSetup){
		distributionRuleSetupService.update(distributionRuleSetup);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/distributionRuleSetup/remove")
	@ResponseBody
	@RequiresPermissions("fx:distributionRuleSetup:remove")
	public R remove( Long ruleId){
		if(distributionRuleSetupService.remove(ruleId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/distributionRuleSetup/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:distributionRuleSetup:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ruleIds){
		distributionRuleSetupService.batchRemove(ruleIds);
		return R.ok();
	}
	
}
